package com.shimao.celnet.payment.controller.apinotcheck;

import com.baiwang.bop.respose.entity.InvoiceInvalidResponse;
import com.shimao.celnet.payment.model.PInvoiceBlue;
import com.shimao.celnet.payment.model.PInvoiceRed;
import com.shimao.celnet.payment.util.HttpSendPostUtil;
import com.shimao.celnet.payment.util.SignCheckUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Iterator;
import java.util.TreeMap;

/**
 * 作废结果通知
 * 2018/01/09
 * create by stan.ruan
 * 世茂支付平台 → 外部系统；
 * 【数据】：作废结果信息；
 */
public class CallOut_outInvldRetInfoOutController {
    /**
     * 作废结果通知
     * @param pInvoiceBlue  作废蓝票
     * @param pInvoiceRed  作废红票
     */


    protected Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private HttpSendPostUtil httpSendPostUtil;
    /**
     * 作废结果通知
     * @param pInvoiceBlue  开蓝票
     * @param pInvoiceRed  开红票
     * @param response  作废结果
     */
    public void invInvlidRetInfoOutController(String returnUrl, InvoiceInvalidResponse response, PInvoiceBlue pInvoiceBlue, PInvoiceRed pInvoiceRed) throws Exception {
        String blueRedType = "";//开票类型
        String appkey = "";//应用编号
        String interfaceId = "";//接口编号
        if(pInvoiceBlue != null){
            blueRedType = "blue";
            appkey = pInvoiceBlue.getAppKey();
            interfaceId = pInvoiceBlue.getInterfaceId();
        }
        if(pInvoiceRed!= null){
            blueRedType = "red";
            appkey = pInvoiceRed.getAppKey();
            interfaceId = pInvoiceRed.getInterfaceId();
        }
        logger.info("应用编号appkey:"+appkey);
        logger.info("接口编号interfaceId:"+interfaceId);
        try {
            String ret = httpSendPostUtil.sendPost(returnUrl, getRetJson(response, blueRedType, pInvoiceBlue, pInvoiceRed),appkey,interfaceId);
            logger.info("通知接口返回数据：" + ret);
        }catch(Exception e){
            e.printStackTrace();
            logger.info("通知异常信息："+"开票结果通知出现异常！");
        }
    }
    /**
     * 拼接开票返回信息的json
     *
     * @return
     */
    public JSONObject getRetJson(InvoiceInvalidResponse response,String blueRedType,PInvoiceBlue pInvoiceBlue, PInvoiceRed pInvoiceRed) throws Exception {
        String  invGuid = "";//每条结果的id
        JSONObject jsonObject = new JSONObject();
        if("red".equals(blueRedType)){
            invGuid = pInvoiceRed.getGuid();
        }else if("blue".equals(blueRedType)) {
            invGuid =pInvoiceBlue.getGuid();
        }
        jsonObject.put("receiptGuid", invGuid);//需要开发票的支付系统的发票guid：发票种类=blue：蓝票；蓝票的支付平台数据guid；发票种类=red：红票：红票的支付平台数据guid；
        jsonObject.put("blueRedType", blueRedType);//发票种类：blue：蓝票；red：红票
        jsonObject.put("invoiceCode", response.getInvoiceCode());//发票代码
        jsonObject.put("invoiceNo", response.getInvoiceNo());//发票号码
        jsonObject.put("invoiceDate", response.getInvoiceInvalidDate());//作废日期

        //生成签名：把所有数据进行拼接，然后在数据的最前面的最后面加上token进行一次md5加密
        String sign = SignCheckUtil.postSign(toMap(jsonObject),"");
        //换成新的签名
        jsonObject.put("sign", sign);
        return jsonObject;
    }
    private TreeMap<String, String> toMap(JSONObject json){
        TreeMap<String, String> map = new TreeMap<String, String>();
        Iterator<String> keys = json.keys();
        while (keys.hasNext()){
            String key = keys.next();
            String value = json.get(key).toString();
            map.put(key, value);
        }
        return map;
    }
}
